﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
public partial class WebCommon_DepartmentSlave : System.Web.UI.Page
{
    DepartmentInfoManager dllDepartmentInfoManager = new DepartmentInfoManager();
    UserInfoManager dllUserInfoManager = new UserInfoManager();
    Authentication dllAuthentication = new Authentication();
    JobManager dllJobManager = new JobManager();
    RoleManager dllRoleManager = new RoleManager();
    public string _target = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        _target = Utility.HttpHelp.getParamsString("SID");

        if (!IsPostBack)
        {
            bindTreeView();
            Bind();
        }
    }

    void Bind()
    {
        if (Request.QueryString["PlanId"] != null && Request.QueryString["PlanType"] != null)
        {
            int PlanId = Utility.HttpHelp.getParamsInt("PlanId");
            string sql = string.Empty;
            DataTable dt = new DataTable();
            int PlanType = Utility.HttpHelp.getParamsInt("PlanType");

            switch (PlanType)
            {
                case 1:
                    sql = "select * from DS_QuarterPlan where QuarterPlanId = " + PlanId;
                    dt = DAL.SqlHelper.ExecuteDataset(DAL.SqlHelper.connStr, CommandType.Text, sql).Tables[0];
                    break;
                case 2:
                    sql = "select * from DS_MonthPlan where MonthPlanId = " + PlanId;
                    dt = DAL.SqlHelper.ExecuteDataset(DAL.SqlHelper.connStr, CommandType.Text, sql).Tables[0];
                    break;
                case 3:
                    sql = "select * from DS_WeekPlan where WeekPlanId = " + PlanId;
                    dt = DAL.SqlHelper.ExecuteDataset(DAL.SqlHelper.connStr, CommandType.Text, sql).Tables[0];
                    break;
            }
            if (dt.Rows.Count > 0)
            {

                SqlParameter[] parameters = new SqlParameter[1];
                string[] RealNames = dt.Rows[0]["SlaveDepartment"].ToString().Split(',');
                string RealName = string.Empty;
                foreach (string var in RealNames)
                {
                    ListItem li = new ListItem(var);
                    this.lbxUserNameOK.Items.Add(li);
                }

            }
        }
    }


    /// <summary>
    /// 绑定TreeView,同时标明状态
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-1-15</date>
    void bindTreeView()
    {
        DataTable _department = dllDepartmentInfoManager.get();
        string _companyName = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        TreeNode _root = new TreeNode(_companyName);
        this.tvDepartment.Nodes.Add(_root);
        for (int i = 0; i < _department.Rows.Count; i++)
        {
            TreeNode _departmentNode;
            if (_department.Rows[i]["IsDelete"].ToString() == "1")
            {
                _departmentNode = new TreeNode(_department.Rows[i]["DepartmentName"].ToString() + "(停用状态)");
            }
            else
            {
                _departmentNode = new TreeNode(_department.Rows[i]["DepartmentName"].ToString());
            }
            _departmentNode.Value = _department.Rows[i]["DepartmentId"].ToString();
            _root.ChildNodes.Add(_departmentNode);
            RecursionTreeNode(_departmentNode);
        }

        TreeNode _singleDepartment = new TreeNode("独立完成");
        this.tvDepartment.Nodes.Add(_singleDepartment);
        TreeNode _slaveDepartment = new TreeNode("全部部门");
        this.tvDepartment.Nodes.Add(_slaveDepartment);

    }

    /// <summary>
    /// 递归方法
    /// </summary>
    void RecursionTreeNode(TreeNode _parent)
    {
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(_parent.Value);
        DataTable _department = dllDepartmentInfoManager.get(DepartmentInfoManager.field.DepartmentParentId.ToString(), _value);
        for (int i = 0; i < _department.Rows.Count; i++)
        {
            TreeNode _child = new TreeNode();
            _child.Text = _department.Rows[i]["DepartmentName"].ToString();
            _child.Value = _department.Rows[i]["DepartmentId"].ToString();
            _parent.ChildNodes.Add(_child);
            RecursionTreeNode(_child);
        }
    }

    /// <summary>
    /// TreeView选择项添加到ListBox
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-12</date>
    protected void tvDepartment_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeNode _tnSelect = this.tvDepartment.SelectedNode;
        if (_tnSelect == null)
        {
            return;
        }
        else
        {
            if (_tnSelect.Text == "独立完成" || _tnSelect.Text == "全部部门")
            {
                ListItem _li = new ListItem(_tnSelect.Text);
                if (!this.lbxUserNameOK.Items.Contains(_li))
                {
                    this.lbxUserNameOK.Items.Add(_li);
                }
            }
            else
            {
                ListItem _li = new ListItem(_tnSelect.Text);
                if (!this.lbxUserNameOK.Items.Contains(_li))
                {
                    this.lbxUserNameOK.Items.Add(_li);
                }
                //遍历选择所有子项
                RecursionSelectTreeNode(_tnSelect);
            }
        }
        this.tvDepartment.SelectedNode.Selected = false;
    }

    /// <summary>
    /// 递归方法
    /// </summary>
    void RecursionSelectTreeNode(TreeNode _parent)
    {
        foreach (TreeNode tn in _parent.ChildNodes)
        {
            ListItem _li = new ListItem(tn.Text);
            if (!this.lbxUserNameOK.Items.Contains(_li))
            {
                this.lbxUserNameOK.Items.Add(_li);
            }
            RecursionSelectTreeNode(tn);
        }
    }



    /// <summary>
    /// 提交事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOk_Click(object sender, EventArgs e)
    {
        string _userInfo = "";
        for (int i = 0; i < this.lbxUserNameOK.Items.Count; i++)
        {
            ListItem li = this.lbxUserNameOK.Items[i];
            _userInfo += li.Text + ",";
        }
        if (_userInfo.Length > 0)
        {
            _userInfo = _userInfo.Remove(_userInfo.Length - 1);
            ScriptManager.RegisterClientScriptBlock(this.btnOk, this.btnOk.GetType(), "userinfo", "GetPro('" + _userInfo + "');", true);
        }
        else
        {
            Utility.HttpHelp.alert("请选择员工", Page);
        }
    }

    /// <summary>
    /// 清空ListBox事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click1(object sender, EventArgs e)
    {
        this.lbxUserNameOK.Items.Clear();
    }





    /// <summary>
    /// 移除按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-26</date>
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        ListItem _li = this.lbxUserNameOK.SelectedItem;
        if (_li != null)
        {
            this.lbxUserNameOK.Items.Remove(_li);
        }
        this.lbxUserNameOK.SelectedIndex = -1;
    }



    //向上
    protected void Button1_Click(object sender, EventArgs e)
    {

        int index = -1;
        //判断哪个ListBox被操作
        if (this.lbxUserNameOK.SelectedIndex >= 0)
        {
            index = this.lbxUserNameOK.SelectedIndex;
            if (index != 0)
            {
                //获得item
                ListItem item = this.lbxUserNameOK.Items[index];

                if (index + 1 > this.lbxUserNameOK.Items.Count)
                {
                }
                else
                {
                    //执行
                    this.lbxUserNameOK.Items.Remove(this.lbxUserNameOK.Items[index]);
                    this.lbxUserNameOK.Items.Insert(index - 1, item);
                }
            }
            this.lbxUserNameOK.SelectedIndex = -1;
        }
        else
        {
            index = this.lbxUserNameOK.SelectedIndex;
            if (index != 0)
            {
                ListItem item = this.lbxUserNameOK.Items[index];

                if (index + 1 > this.lbxUserNameOK.Items.Count)
                {
                }
                else
                {
                    this.lbxUserNameOK.Items.Remove(this.lbxUserNameOK.Items[index]);
                    this.lbxUserNameOK.Items.Insert(index - 1, item);
                }
            }
        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int index = -1;
        //判断哪个ListBox被操作
        if (this.lbxUserNameOK.SelectedIndex >= 0)
        {
            index = this.lbxUserNameOK.SelectedIndex;
            if (index >= 0)
            {
                //获得item
                ListItem item = this.lbxUserNameOK.Items[index];

                if (index + 1 >= this.lbxUserNameOK.Items.Count)
                {
                }
                else
                {
                    //执行
                    this.lbxUserNameOK.Items.Remove(this.lbxUserNameOK.Items[index]);
                    this.lbxUserNameOK.Items.Insert(index + 1, item);
                }
            }
            this.lbxUserNameOK.SelectedIndex = -1;
        }
        else
        {
            index = this.lbxUserNameOK.SelectedIndex;
            if (index >= 0)
            {
                ListItem item = this.lbxUserNameOK.Items[index];

                if (index + 1 >= this.lbxUserNameOK.Items.Count)
                {
                }
                else
                {
                    this.lbxUserNameOK.Items.Remove(this.lbxUserNameOK.Items[index]);
                    this.lbxUserNameOK.Items.Insert(index + 1, item);
                }
            }
        }
    }


    /// <summary>
    /// 向上
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        int index = -1;
        //判断哪个ListBox被操作
        if (this.lbxUserNameOK.SelectedIndex >= 0)
        {
            index = this.lbxUserNameOK.SelectedIndex;
            if (index != 0)
            {
                //获得item
                ListItem item = this.lbxUserNameOK.Items[index];

                if (index + 1 > this.lbxUserNameOK.Items.Count)
                {
                }
                else
                {
                    //执行
                    this.lbxUserNameOK.Items.Remove(this.lbxUserNameOK.Items[index]);
                    this.lbxUserNameOK.Items.Insert(index - 1, item);
                }
            }
            this.lbxUserNameOK.SelectedIndex = -1;
        }
        else
        {
            index = this.lbxUserNameOK.SelectedIndex;
            if (index != 0)
            {
                ListItem item = this.lbxUserNameOK.Items[index];

                if (index + 1 > this.lbxUserNameOK.Items.Count)
                {
                }
                else
                {
                    this.lbxUserNameOK.Items.Remove(this.lbxUserNameOK.Items[index]);
                    this.lbxUserNameOK.Items.Insert(index - 1, item);
                }
            }
        }

    }
    /// <summary>
    /// 向下
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
        int index = -1;
        //判断哪个ListBox被操作
        if (this.lbxUserNameOK.SelectedIndex >= 0)
        {
            index = this.lbxUserNameOK.SelectedIndex;
            if (index >= 0)
            {
                //获得item
                ListItem item = this.lbxUserNameOK.Items[index];

                if (index + 1 >= this.lbxUserNameOK.Items.Count)
                {
                }
                else
                {
                    //执行
                    this.lbxUserNameOK.Items.Remove(this.lbxUserNameOK.Items[index]);
                    this.lbxUserNameOK.Items.Insert(index + 1, item);
                }
            }
            this.lbxUserNameOK.SelectedIndex = -1;
        }
        else
        {
            index = this.lbxUserNameOK.SelectedIndex;
            if (index >= 0)
            {
                ListItem item = this.lbxUserNameOK.Items[index];

                if (index + 1 >= this.lbxUserNameOK.Items.Count)
                {
                }
                else
                {
                    this.lbxUserNameOK.Items.Remove(this.lbxUserNameOK.Items[index]);
                    this.lbxUserNameOK.Items.Insert(index + 1, item);
                }
            }
        }
    }


    protected void lbxUserNameOK_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
